$(function() {
	load();	
	//查询排产单日期
	selectDaysheduTime();
	
	//查询固化区的台车总数
	 selectTc();
	//查询固化区的组件总数
	 selectZc();
	 
	//查询待切割台车列表
	 selectList2();
	 
	 setIntervalDemo();
	 
    var product = $("#product").val();
	if(product == "" || product == undefined || product == null) {
		return;
	}else {
    	selectNum();
    	selectYearNum();
	}
	
})

    //查询生产计划量
	function selectNum() {
    	var position = "切割";
    	var product = $("#product").text();
    	var format = $.trim($("#planTime").text());
    	 $.ajax({
	    	type:'post',
	    	async:false, 
	    	url:'/shedu/selectByDate.do',
	    	data: {"product":product,"position":position,"format":format},
	    	success:function(data){
	    		$("#planNum").html(data);
	    		$.ajax({
			    	type:'post',
			    	async:false, 
			    	url:'/cut/selectActualNum.do',
			    	data: {"product":product},
			    	success:function(data){
			    		$("#actualNum").html(data);
			    	}
			    });
	    		selectYearNum();
	    	}
	    })
	}
	
	 //统计生产量
	 function selectActualNum() {
    	var product = $("#product").text();
    	 $.ajax({
	    	type:'post',
	    	async:false, 
	    	url:'/cut/selectActualNum.do',
	    	data: {"product":product},
	    	success:function(data){
	    		$("#actualNum").html(data);
	    	}
	    })
	}
	 
	 //统计年产品序列
	function selectYearNum() {
    	var product = $("#product").text();
    	 $.ajax({
	    	type:'post',
	    	async:false, 
	    	url:'/cut/selectYearNum.do',
	    	data: {"product":product},
	    	success:function(data){
	    		$("#yearNum").html(data);
	    	}
	    })
	}
	 
	 function altRows(id){
		if(document.getElementsByTagName){  
			
			var table = document.getElementById(id);  
			var rows = table.getElementsByTagName("tr"); 
			 
			for(i = 0; i < rows.length; i++){          
				if(i % 2 == 0){
					rows[i].className = "evenrowcolor";
				}else{
					rows[i].className = "oddrowcolor";
				}      
			}
		}
	}

		
	var id = "g1"
	function keyDown(e){  
	    //IE内核浏览器  
	     if (navigator.appName == 'Microsoft Internet Explorer'){  
	           var keycode = event.keyCode;  
	           var realkey = String.fromCharCode(event.keyCode);  
	     }else {//非IE内核浏览器  
	           var keycode = e.which;  
	           var realkey = String.fromCharCode(e.which);  
	     }  

	   //监听Ctrl键  
	   if(keycode==13){  
		   
		   //配件报检的条码
		   var icode =  $("#icode").val();
		   if(icode != null && icode!= "" && icode != undefined) {
			  $("#icode").val(icode+",");
			   return;
		   }
		  
		   //切割的条码
		   var ctube=$.trim($("#g1").val());
		   if(ctube != null && ctube != "" && ctube != undefined) {
			   var str = ctube.substr(3,4);
			   if(str == "0960") {
					$("#product").html("PSH2-0960-T-01");
				}else if(str == "0980") {
					$("#product").html("PSH2-0980-T-02");
				}else if(str == "1060") {
					$("#product").html("PSH1-1060-B-03");
				}else if(str == "1080") {
					$("#product").html("PSH1-1080-C-04");
				}
			   	selectNum();
		    	selectYearNum();
		   }
		   
		   var cremark1=$.trim($("#cremark1").val());
		   var cremark2=$.trim($("#cremark2").val());
		   var cplan=$.trim($("#planTime").val());
		   
		   if(ctube.length != 16 || ctube.substring(1,3) != "PS"){
				toastr.warning("请扫码正确的膜管编号！");
				return;
		   }
		   
		   if(ctube == "" || ctube == undefined || ctube == null) {
	  			return;
	  		}else {
	  			 $.ajax({
	  		    	type:'post',
	  		    	url:'/cut/save.do',
	  		    	data: {"ctube":ctube,"cremark1":cremark1,"cremark2":cremark2,"cplan":cplan},
	  		    	success:function(data){
	  		    		if(data>0){
	  		    			toastr.success("固化结束，开始切割！");
	  		    		}else if(data == -1) {
	  		    			toastr.warning("该膜管已切割完成！请扫描其他膜管");
	 		    			$("#g1").select();
	  		    		}else if(data == -2) {
	  		    			toastr.success("该膜管已开始切割！不要重复扫码");
	  		    		}else if(data == -3) {
	  		    			toastr.error("该膜管固化结束失败！");
	  		    			$("#g1").select();
	  		    		}else if(data == -4) {
	  		    			toastr.warning("对不起，该膜管未固化，不能进行切割！");
	  		    			$("#g1").select();
	  		    		}else if(data == -5) {
	  		    			toastr.warning("对不起，该膜管未开始固化，不能进行切割！");
	  		    			$("#g1").select();
	  		    		}else if(data == -6) {
	  		    			toastr.warning("对不起，固化时间不够，不能进行切割！");
	  		    			$("#g1").select();
	  		    		}else{
	  		    			toastr.error("操作失败！请切换英文输入法试试");
	  		    			$("#g1").select();
	  		    		}
	  		    	}
	  		    })
	  		}
	   }   
	   $("#"+id).change(function(){  
	       console.log($(this).val());  
	       $("#"+id).select();  
	   })  
	   
	}  
	//按键事件
	document.onkeydown=keyDown;
	
	 //查询台车总数
    function selectTc() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectTcount2.do',
	    	success:function(data){
	    		$("#tc").html(data);
	    	}
	    });
	}
    
    //查询组件总数
    function selectZc() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectZcount2.do',
	    	success:function(data){
	    		$("#zc").html(data);
	    	}
	    });
    }
	
    //查询所有待切割的台车列表
    function selectList2() {
    	$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectList2.do',
	    	success:function(data){
	    		if(data[0] != null && data[0] != "" && data[0] != undefined ) {
	    			
	    			for(var i = 1; i < data.length+1; i++) {
	    				//台车
			    		$("#c"+i).html(data[i-1].cremark);
			    		//组件数量
			    		$("#z"+i).html(data[i-1].cremark4);
			    		//4个组件详情
			    		$("#s"+i).html(data[i-1].cremark7);
			    		//所有组件详情
			    		$("#m"+i).html(data[i-1].cremark8);
	    			}
	    			if(5> data.length) {
	    				var size = data.length;
	    				var x = 5 - size
	    				for(var i = 0; i < x; i++) {
	    					var j = 5-i;
		    				//台车
				    		$("#c"+j).html("");
				    		//组件数量
				    		$("#z"+j).html("");
				    		//4个组件详情
				    		$("#s"+j).html("");
				    		//所有组件详情
				    		$("#m"+j).html("");
		    			}
	    			}
	    		}else {
	    			$("#c"+1).html("");
		    		//组件数量
		    		$("#z"+1).html("");
		    		//4个组件详情
		    		$("#s"+1).html("");
		    		//所有组件详情
		    		$("#m"+1).html("");
	    		}
	    	}
	    });
	}
    
    //查询排产单日期
    function selectDaysheduTime() {
    	$.ajax({
    		type:'post',
    		url:'/solid/selectDaysheduTime.do',
    		success:function(data){
    			$("#planTime").html(data);
    		}
    	});
    }

    
    /*AGV拉动*/
	function agv() {
		$.ajax({
	    	type:'post',
	    	url:'/cutAgv.do',
	    	success:function(data){
	    		if(data == 1) {
	    			toastr.success("指令发送成功，等待AGV响应!");
	    		}else {
	    			toastr.err("AGV拉动失败！");
	    		}
	    	}
	    });
	}
	
	 //定时器
    function setIntervalDemo(){
        setTimeoutName = setInterval(function(){
        	
        	 $.ajax({
   			  	async:false, 
   		    	type:'post',
   		    	url:'/getCut.do',
   		    	success:function(data){
   		    		if(data == 1) {
   		    			//toastr.success("agv已接收指令");
   		    		 $.ajax({
   		   			  	async:false, 
   		   		    	type:'post',
   		   		    	url:'/setCut.do',
   		   		    	success:function(data){
   		   		    		if(data == 1) {
   		   		    			toastr.success("agv已接收指令");
   		   		    		}
   		   		    	}
   		        	 });
   		    		}
   		    	}
        	 });
        },1000);
    }
